package com.tongji.housediy.view
{
	import com.tongji.housediy.events.SetShowNameEnable;
	import com.tongji.housediy.events.UpdateRatioCompEvent;
	import com.tongji.housediy.events.UpdateViewControlEvent;
	import com.tongji.housediy.events.commonEvent.ChangeData3D;
	import com.tongji.housediy.events.commonEvent.RoomNameAdminEvent;
	import com.tongji.housediy.events.commonEvent.RulerAdminEvent;
	import com.tongji.housediy.events.commonEvent.ShowComplementInforEvent;
	import com.tongji.housediy.events.commonEvent.ShowHideControlPanelEvent;
	import com.tongji.housediy.events.commonEvent.ShowHideTitleWindowEvent;
	import com.tongji.housediy.model.CompEnableStateModel;
	import com.tongji.housediy.utils.ShowCustomToolTip;
	import com.tongji.housediy.view.DrawingBoardUpComp;
	import com.tongji.housediy.view.components.AbstractDrawingRoom;
	
	import flash.display.BitmapData;
	import flash.events.MouseEvent;
	
	import mx.binding.utils.BindingUtils;
	import mx.controls.Alert;
	import mx.controls.Image;
	import mx.core.BitmapAsset;
	import mx.core.DragSource;
	import mx.core.IFlexDisplayObject;
	import mx.core.UIComponent;
	import mx.events.FlexEvent;
	import mx.events.MoveEvent;
	import mx.managers.DragManager;
	
	import org.robotlegs.mvcs.Mediator;
	
	import spark.components.Button;
	
	/**
	 * 
	 * @author chenchao
	 * 
	 */
	public class DrawingBoardUpMediator extends Mediator
	{
		[Inject]
		public var view:DrawingBoardUpComp;
		private var showNameClick:int = 0;
		
		override public function onRegister():void
		{
			this.addContextListener(ShowHideControlPanelEvent.SHOW_CONTROL_PANEL, showControlPanelHandler, ShowHideControlPanelEvent);
			this.addContextListener(ShowHideControlPanelEvent.HIDE_CONTROL_PANEL, hideControlPanelHandler, ShowHideControlPanelEvent);
			this.addContextListener(UpdateViewControlEvent.TWO_DIMENSION_VIEW, changeViewStateHandler, UpdateViewControlEvent);
			this.addContextListener(UpdateViewControlEvent.THREE_DIMENSION_VIEW, changeViewStateHandler, UpdateViewControlEvent);
			this.addContextListener(ShowComplementInforEvent.SHOW_COMPLEMENT_INFOR, showComplementinfor, ShowComplementInforEvent);
			
			view.rulerBtn.addEventListener(MouseEvent.CLICK, rulerButtonClickHandler);
			view.showNameBtn.addEventListener(MouseEvent.CLICK, showNameBtnClickHandler);
			view.parent.addEventListener(RoomNameAdminEvent.HIDE_ROOM_NAME, showNameBtnResetHandler);
			this.addContextListener(UpdateRatioCompEvent.UPATE_RATIO_COMP, updateRatioCompHandler, UpdateRatioCompEvent);
			this.addContextListener(SetShowNameEnable.SET_SHOW_NAME_ENABLE, setShowNameEnableHandler, SetShowNameEnable);
			this.addContextListener(SetShowNameEnable.SET_SHOW_NAME_UNABLE, setShowNameUnableHandler, SetShowNameEnable);
		}

		private function setShowNameEnableHandler(event:SetShowNameEnable):void
		{
			this.showNameClick = event.roomNum;
			if(event.roomNum ==0)
				view.showNameBtn.selected = false;
		}
		
		private function setShowNameUnableHandler(event:SetShowNameEnable):void
		{
			view.showNameBtn.selected = false;
		}
		
		private function showNameBtnResetHandler(event:RoomNameAdminEvent):void
		{
			view.showNameBtn.selected = false;
		}
		private function showComplementinfor(event:ShowComplementInforEvent):void
		{
			var showToolTip:ShowCustomToolTip = new ShowCustomToolTip(view.rulerBtn, "显示尺寸");
		}
		
		private function showControlPanelHandler(event:ShowHideControlPanelEvent):void
		{
			if (view.currentState == "hide2D")
				view.currentState = "initial2D";
			else
				view.currentState = "initial3D";
		}
		
		private function hideControlPanelHandler(event:ShowHideControlPanelEvent):void
		{
			if (view.currentState == "initial2D")
				view.currentState = "hide2D";
			else
				view.currentState = "hide3D";
		}
		
		private function rulerButtonClickHandler(event:MouseEvent):void
		{
			dispatch(new RulerAdminEvent(RulerAdminEvent.SHOW_HIDE_RULERS));
		}
		
		private function showNameBtnClickHandler(event:MouseEvent):void
		{
			if(this.showNameClick > 0)
			{
				dispatch(new RoomNameAdminEvent(RoomNameAdminEvent.SHOW_HIDE_ROOM_NAME));
				dispatch(new RulerAdminEvent(RulerAdminEvent.HIDE_ALL_RULERS));
				dispatch(new ShowHideTitleWindowEvent(ShowHideTitleWindowEvent.HIDE_TITLE_WINDOW));
			}
			else
			{
				view.showNameBtn.selected = false;
			}
		}
		
		private function changeViewStateHandler(event:UpdateViewControlEvent):void
		{
			switch (view.currentState)
			{
				case "initial2D":
					view.currentState = "initial3D";
					break;
				case "initial3D":
					view.currentState = "initial2D";
					break;
				case "hide2D":
					view.currentState = "hide3D";
					break;
				case "hide3D":
					view.currentState = "hide2D";
					break;
			}
		}
		
		private function updateRatioCompHandler(event:UpdateRatioCompEvent):void
		{
			view.ratioComp.changeSize(event.scaleMultiple);
		}
	}
}